package homework;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author DCSGO
 * @version 1.0
 * 1.定义PrivateTest类，有私有name属性，并且属性值为 hello CS2
 * 2.提供getName的公有方法
 * 3.创建PrivateTest的类，利用Class类得到私有的name属性，修改私有的name属性值,
 * 并调用getName()的方法打印name属性值
 */
public class Homework01 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class<PrivateTest> cls = PrivateTest.class;
        Field name = cls.getDeclaredField("name");
        name.setAccessible(true);
        Object o = cls.newInstance();
        name.set(o, "Hi CS2");
        Method getName = cls.getMethod("getName");
        System.out.println(getName.invoke(o));//Hi CS2
    }
}

class PrivateTest {
    private String name = "hello CS2";

    public String getName() {
        return name;
    }
}
